using System.Linq;
using HashFoo.Nhib;
using HashFoo.NhibTests.DomainFixtures.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Linq;

namespace HashFoo.NhibTests
{
    [TestClass]
    public class SessionUtilTests
    {
        private static ISessionFactory _sessionFactory;

        [ClassInitialize]
        public static void Setup(TestContext context)
        {
            _sessionFactory = DomainFixtures.MockConfigurator.DefaultConfiguration.BuildSessionFactory();
        }

        [ClassCleanup]
        public static void TearDown()
        {
            if (_sessionFactory == null || _sessionFactory.IsClosed) return;

            _sessionFactory.Close();
            _sessionFactory.Dispose();
        }

        [TestMethod]
        [Description("Makes sure a DbEnum can be ensured")]
        public void CanEnsureDbEnum()
        {
            Helpers.NukeDatabase();
            Helpers.CreateDefaultMigrationManagerWorker().Create();

            using (var session = _sessionFactory.OpenSession())
            {
                Assert.IsTrue(session.Query<PlantType>().Count() == 0);

                var plantType = session.EnsureDbEnum(new PlantType("corn") { MaxHeightMeters = 1.5 });

                session.Flush();
                session.Clear();
                Assert.IsTrue(session.Query<PlantType>().Count() == 1);

                session.EnsureDbEnum(plantType);
                session.Flush();
                Assert.IsTrue(session.Query<PlantType>().Count() == 1);

                session.Close();
            }
        }
    }
}